#pragma once
#include<iostream>
#include<string>
#include<sys/types.h>
#include<sys/socket.h>

class Util
{
    public:
        static int ReadLine(int sock,std::string &out)
        {
            char ch='x';
            while(ch!='\n')
            {
                // recv 默认把数据从接收缓冲区中读取并把数据取走
                 ssize_t s=recv(sock,&ch,1,0);
                 if(s>0)
                 {
                    if(ch=='\r')
                    {
                        recv(sock,&ch,1,MSG_PEEK);
                        if(ch=='\n')
                        {
                            recv(sock,&ch,1,0);
                        }
                        else
                        {
                            ch='\n';
                        }
                    }
                    out.push_back(ch);
                 }
                 else if(s==0)
                 {
                    return 0;
                 }
                 else
                 {
                    return -1;
                 }
            }
            return out.size();
        }
        static bool CutString(const std::string &target,std::string &substr1,std::string &substr2,std::string sep)
        {
            size_t pos=target.find(sep);
            if(pos!=std::string::npos)
            {
                substr1=target.substr(0,pos);
                substr2=target.substr(pos+sep.size(),std::string::npos);
                return true;
            }
            return false;
        }
};